package com.ky.tms.controller;


import com.ky.tms.entity.TmsCost;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.service.ITmsCostService;
import com.ky.tms.util.CommonResult;
import com.ky.tms.util.JwtUtil;
import com.ky.tms.vo.CostFinish;
import com.ky.tms.vo.CostOrders;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chengkaimao
 * @since 2020-03-24
 */
@RestController
@RequestMapping("/tms/tms-cost")
public class TmsCostController {

    @Autowired
    private ITmsCostService tmsCostService;

    @ApiOperation("费用分页数据")
    @GetMapping("dispatch-page")
    public CommonResult dispatchPage(TmsCost tmsCost, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request) {
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsCostService.dispatchPage(userName,tmsCost,pageSize, current));
    }

    @ApiOperation("审核费用分页数据")
    @GetMapping("audit-page")
    public CommonResult auditPage(TmsCost tmsCost, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request) {
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsCostService.auditPage(userName,tmsCost,pageSize, current));
    }

    @ApiOperation("保存费用")
    @PostMapping
    public CommonResult save(@RequestBody TmsCost tmsCost, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        tmsCostService.costAdd(userName, tmsCost);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("删除费用")
    @DeleteMapping
    public CommonResult del(@RequestBody TmsCost tmsCost, HttpServletRequest request)throws PredictableException{
        String userName = JwtUtil.getUserName(request);
        tmsCostService.costDel(userName,tmsCost);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("保存费用")
    @PostMapping("cost-orders")
    public CommonResult costOrders(@RequestBody CostOrders costOrders, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsCostService.costOrders(userName, costOrders);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("保存申请")
    @PostMapping("cost-apply")
    public CommonResult applyCost(@RequestBody TmsCost tmsCost, HttpServletRequest request) {
        String userName = JwtUtil.getUserName(request);
        tmsCostService.applyCost(userName, tmsCost);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("更新费用")
    @PutMapping
    public CommonResult update(@RequestBody TmsCost tmsCost, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsCostService.costUpdate(userName, tmsCost);
        return CommonResult.buildSuccess(null);
    }


    @ApiOperation("审核费用")
    @PostMapping("audit-cost")
    public CommonResult auditCost(@RequestBody List<TmsCost> tmsCost, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsCostService.auditCost(userName, tmsCost);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("申请支付")
    @PostMapping("applyPay")
    public CommonResult applyPay(@RequestBody List<TmsCost> tmsCosts, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsCostService.applyPay(userName, tmsCosts);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("紧急支付")
    @PostMapping("urgentPay")
    public CommonResult urgentPay(@RequestBody TmsCost tmsCost, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsCostService.urgentCost(userName, tmsCost);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("完成支付")
    @PostMapping("finish")
    public CommonResult finish(@RequestBody CostFinish costFinish, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsCostService.finishCost(userName, costFinish);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("驳回支付")
    @PostMapping("reject")
    public CommonResult reject(@RequestBody TmsCost tmsCost)throws PredictableException{
        tmsCostService.rejectCost(tmsCost);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("合计费用")
    @GetMapping("sumFee")
    public CommonResult sumFee(TmsCost tmsCost){
        return CommonResult.buildSuccess(tmsCostService.sumFee(tmsCost));
    }

    @ApiOperation("修改账户")
    @PostMapping("updateAccount")
    public CommonResult updateAccount(@RequestBody TmsCost tmsCost) throws PredictableException {
        tmsCostService.updateAccount(tmsCost);
        return CommonResult.buildSuccess(null);
    }

}
